package com.lgx.mall.business.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.web.Pagination;
import com.lgx.mall.business.dao.IOrderDao;
import com.lgx.mall.business.entity.Order;

@Component
public class OrderDao extends GeneralDao implements IOrderDao{
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;

	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}


	/**
	 * o.id : 订单编号
	 * o.shoppingTime : 提交时间
	 * o.payTime : 付款时间
	 * o.score : 订单积分总额
	 * s.id : 会员卡号/用户卡号
	 * o.consigneeName : 收货人
	 * o.consigneeTel : 收货人电话
	 * o.status : 状态
	 * o.fare : 运费
	 * u.isDelete = 0 ,标示这个用户/会员可用，系统才执行查询
	 */
	@Override
	public Pagination findOrderPagination(String username, String usercard,
			String orderNumber, String starTime, String endTime,String status, int pageSize,
			int pageNum) {
		
		String sql = " from Order o where o.status > 0 ";
		
		if(!StringUtils.isEmpty(username)){   //用户姓名
			sql += " and o.user.username like '%"+username+"%'";
		}
		
		if(!StringUtils.isEmpty(usercard)){   //用户卡号
			sql += " and o.user.card.id like '%"+usercard+"%'";
		}
		
		if(!StringUtils.isEmpty(orderNumber)){  //订单号
			sql += " and o.id like '%"+orderNumber+"%'";
		}
		
		if(!StringUtils.isEmpty(starTime)){     //提交时间
			sql += " and o.shoppingTime >="+starTime;
		}
		
		if(!StringUtils.isEmpty(endTime)){      //付款时间
			sql += " and o.payTime <="+endTime;
		}
		
		sql += " order by o.id desc";
		
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

	/**
	 * 查找
	 */
	@Override
	public Order findOrderById(Long orderId) {
		return this.getHibernateTemplate().get(Order.class, orderId);
	}

	@Override
	@Transactional
	public void deleteOrder(Order order) {
		log.info("[删除订单]:"+order.getId());
		this.getHibernateTemplate().delete(order);
	}

	@Override
	@Transactional
	public void updateOrder(Order order) {
		log.info("[修改订单]:"+order.getId());
		this.getHibernateTemplate().update(order);
	}

	@Override
	@Transactional
	public Order createOrder(Order order) {
		this.getHibernateTemplate().save(order);
		log.info("[创建订单]:"+order.getId());
		return order;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Order findOrderByUserId(long userId) {
		String sql = "from Order o where o.user.id = " + userId + " and o.status = 0";
		
		List<Order> orders = this.getHibernateTemplate().find(sql);
		
		if(orders.size() > 0){
			return orders.get(0);
		}
		return null;
	}

	@Override
	public Order findOrderByOrderNo(String orderNo) {
		String hql = "from Order o where o.orderNo = '"+orderNo+"'";
		@SuppressWarnings("unchecked")
		List<Order> orders = this.getHibernateTemplate().find(hql);
		
		if(orders.size() > 0){
			return orders.get(0);
		}
		return null;
	}


	@Override
	public Pagination findOrderPagination(Long userId, int pageSize, int pageNum) {
		String sql = "from Order o where o.user.id = " + userId + " and o.status > 0";
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

}
